package com.jc.nio.thread;

import java.net.ServerSocket;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 接受器组<br/>
 * 这是一个Thread线程组管理类，对acceptor的封装，负责对里面的线程进行管理，包括初始化，运行停止
 * 
 * @author Joeson
 * @since 2014/05
 * @version 01
 * 
 */
public class AcceptorGroup
{
	private Acceptor[] acceptors;
	private ServerSocketChannel serverSocket;

	private WorkerGroup workerGroup = null;

	/**
	 * woker处理线程数目
	 */
	private int size;

	/**
	 * worker线程的优先级
	 */
	private int threadPriority = Thread.NORM_PRIORITY;

	public AcceptorGroup()
	{

	}

	public AcceptorGroup(int size, ServerSocketChannel serverSocket,
			WorkerGroup workerGroup)
	{
		this.size = size;
		this.serverSocket = serverSocket;
		this.workerGroup = workerGroup;

		initWorker();
	}

	private void initWorker()
	{
		initAcceptors();
	}

	/**
	 * 负责对acceptor线程的初始化，对accpet线程组里面的线程进行启动，接受来自connect的连接
	 */
	private void initAcceptors()
	{
		acceptors = new Acceptor[size];

		for (int i = 0; i < size; i++)
		{
			acceptors[i] = new Acceptor(serverSocket, workerGroup);
			Thread thread = new Thread(acceptors[i], "acceptor--" + i);
			thread.setDaemon(true);
			thread.setPriority(threadPriority);
			thread.start();
		}
	}

	/**
	 * 使得他的各个acceptor线程停止一段时间
	 */
	public void paused()
	{
		for (int i = 0; i < size; i++)
		{
			acceptors[i].paused();
		}
	}
}
